package com.lxk.controller;

import com.lxk.pojo.Tem;
import com.lxk.service.CouponService;
import com.lxk.vo.CouponInfoModel;
import com.lxk.vo.CouponShowModel;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/8 16:20
 * @description 优惠券控制器
 */
@Controller
public class CouponController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CouponService cs;


    /**
     * 优惠券模板列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/tem")
    public String toTem(HttpServletRequest req, Model model,
                              Integer pageIndex, Integer num, Integer totalPages,
                              Integer sort, Integer order ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 10;
        }
        if (null == totalPages) {
            totalPages = cs.getTemTotalPages(num);
        }
        PageModel<Tem> info = cs.selTemPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", info);
        return "tem";
    }


    /**
     * 优惠券列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/coupon")
    public String toCoupon(HttpServletRequest req, Model model,
                              Integer pageIndex, Integer num, Integer totalPages,
                              Integer sort, Integer order ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 16;
        }
        if (null == totalPages) {
            totalPages = cs.getCouponTotalPages(num);
        }
        PageModel<CouponShowModel> info = cs.selCouponPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", info);
        return "coupon";
    }



    /**
     * 新增优惠券模板
     * @param tem
     * @return
     */
    @ResponseBody
    @PostMapping("/tem/add")
    public Result<Integer> addNewTem(Tem tem) {
        cs.addNewTem(tem);
        return Result.success(1);
    }

    /**
     * 编辑优惠券模板
     * @param tem
     * @return
     */
    @ResponseBody
    @PostMapping("/tem/modify")
    public Result<Integer> modifyTem(Tem tem) {
        cs.updTem(tem);
        return Result.success(1);
    }

    /**
     * 删除优惠券模板
     * @param temId
     * @return
     */
    @ResponseBody
    @GetMapping("/tem/delete")
    public Result<Integer> deleteTem(Integer temId) {
        cs.deleteTem(temId);
        return Result.success(1);
    }

    /**
     * 发放优惠券
     * @param temId
     * @return
     */
    @ResponseBody
    @GetMapping("/tem/send/all")
    public Result<Integer> generateCoupon(Integer temId) {
        Integer num = cs.generateCoupon(temId);
        return Result.success(num);
    }

    /**
     * 查找优惠券
     * @param temId
     * @return
     */
    @ResponseBody
    @GetMapping("/tem/query")
    public Result<Tem> selCoupon(Integer temId) {
        Tem tem = cs.selTemById(temId);
        return Result.success(tem);
    }


    /**
     * 获取用户所有可用优惠券
     * @param wxUserId
     * @return
     */
    @GetMapping("/user/coupon")
    @ResponseBody
    public Result<List<CouponInfoModel>> getUnusedCouponList(Integer wxUserId) {
        List<CouponInfoModel> list = cs.getAllAvailCoupon(wxUserId);
        return Result.success(list);
    }


}
